<?php
    /**
     * <code>
     *		// Get the $array['user']['name'] value from the array
     *		$name = arrayGet($array, 'user.name');
     *
     *		// Return a default from if the specified item doesn't exist
     *		$name = arrayGet($array, 'user.name', 'Taylor');
     * </code>
     */
    function arrayGet($array, $key, $default = null)
    {
        if (is_null($key)) {
            return $array;
        }

        foreach (explode('.', $key) as $segment) {
            if (!is_array($array) || !array_key_exists($segment, $array)) {
                return value($default);
            }
            $array = $array[$segment];
        }
        return $array;
    }
    
    /**
     * <code>
     *		// Set the $array['user']['name'] value on the array
     *		arraySet($array, 'user.name', 'Taylor');
     *
     *		// Set the $array['user']['name']['first'] value on the array
     *		arraySet($array, 'user.name.first', 'Michael');
     * </code>
     */
    function arraySet(&$array, $key, $value)
    {
        if (is_null($key)) {
            return $array = $value;
        }

        $keys = explode('.', $key);
        while (count($keys) > 1) {
            $key = array_shift($keys);
            if (!isset($array[$key]) || !is_array($array[$key])) {
                $array[$key] = array();
            }
            $array =& $array[$key];
        }
        $array[array_shift($keys)] = $value;
    }
    
    /**
     * <code>
     *		// Remove the $array['user']['name'] item from the array
     *		arrayUnset($array, 'user.name');
     *
     *		// Remove the $array['user']['name']['first'] item from the array
     *		arrayUnset($array, 'user.name.first');
     * </code>
     */
    function arrayUnset(&$array, $key)
    {
        $keys = explode('.', $key);
        while (count($keys) > 1) {
            $key = array_shift($keys);
            if ( ! isset($array[$key]) || ! is_array($array[$key]))  {
                return;
            }

            $array =& $array[$key];
        }
        unset($array[array_shift($keys)]);
    }
    
    /**
     * <code>
     *		// Return the first array element that equals "Taylor"
     *		$value = arrayFirst($array, function($k, $v) {return $v == 'Taylor';});
     *
     *		// Return a default value if no matching element is found
     *		$value = arrayFirst($array, function($k, $v) {return $v == 'Taylor'}, 'Default');
     * </code>
     */
    function arrayFirst($array, $callback, $default = null)
    {
        foreach ($array as $key => $value) {
            if (call_user_func($callback, $key, $value)) {
                return $value;
            }
        }
        return value($default);
    }
    
    function arrayMap($callback, $array, $keys = null)
	{
		foreach ($array as $key => $val) {
			if (is_array($val)) {
				$array[$key] = arrayMap($callback, $array[$key]);
			} elseif ( ! is_array($keys) || in_array($key, $keys)) {
				if (is_array($callback)) {
					foreach ($callback as $cb) {
						$array[$key] = call_user_func($cb, $array[$key]);
					}
				} else {
					$array[$key] = call_user_func($callback, $array[$key]);
				}
			}
		}

		return $array;
	}
    
    function arrayUnshift(array & $array, $key, $val)
	{
		$array = array_reverse($array, true);
		$array[$key] = $val;
		$array = array_reverse($array, true);

		return $array;
	}
    
    function arrayStripslashes($array)
    {
        $result = array();
        foreach($array as $key => $value) {
            $key = stripslashes($key);
            if (is_array($value)) {
                $result[$key] = arrayStripslashes($value);
            } else {
                $result[$key] = stripslashes($value);
            }
        }
        return $result;
    }
    
    function arrayDivide($array)
    {
        return array(array_keys($array), array_values($array));
    }
    
    function arrayOnly($array, $keys)
    {
        return array_intersect_key($array, array_flip((array) $keys));
    }
    
    function arrayExcept($array, $keys)
    {
        return array_diff_key($array, array_flip((array) $keys));
    }
    
    function arraySubset($array, $keys)
    {
        return array_intersect_key($array, array_flip((array) $keys));
    }
    
    function arrayPluck($array, $key)
    {
        return array_map(function($v) use ($key) {
            return is_object($v) ? $v->$key : $v[$key];

        }, $array);
    }
    
    function arrayIsAssoc(array $array)
	{
		$keys = array_keys($array);
		return array_keys($keys) !== $keys;
    }
    
    function arrayMerge(array $a1, array $a2)
	{
		$result = array();
		for ($i = 0, $total = func_num_args() ; $i < $total ; $i++)
		{
			$arr = func_get_arg($i);

			$assoc = arrayIsAssoc($arr);

			foreach ($arr as $key => $val) {
				if (isset($result[$key])) {
					if (is_array($val) && is_array($result[$key])) {
						if (arrayIsAssoc($val)) {
							$result[$key] = arrayMerge($result[$key], $val);
						} else {
							$diff = array_diff($val, $result[$key]);
							$result[$key] = array_merge($result[$key], $diff);
						}
					} else {
						if ($assoc) {
							$result[$key] = $val;
						} elseif (!in_array($val, $result, true)) {
							$result[] = $val;
						}
					}
				} else {
					$result[$key] = $val;
				}
			}
		}
		return $result;
	}
    
    function arrayFlatten($array)
	{
		$flat = array();
		foreach ($array as $key => $value) {
			if (is_array($value)) {
				$flat += arrayFlatten($value);
			} else {
				$flat[$key] = $value;
			}
		}
		return $flat;
	}
    
    function isArray($value)
	{
		if (is_array($value)) {
			return true;
		} else {
			// Traversable object is functionally the same as an array
			return (is_object($value) && $value instanceof \Traversable);
		}
	}
    
    function head($array)
    {
        return reset($array);
    }
    
    function magicQuotes()
    {
        return function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc();
    }
    
    function startsWith($haystack, $needle)
    {
        return strpos($haystack, $needle) === 0;
    }
    
    function endsWith($haystack, $needle)
    {
        return $needle == substr($haystack, strlen($haystack) - strlen($needle));
    }
    
    function strContains($haystack, $needle)
    {
        foreach ((array) $needle as $n) {
            if (false !== strpos($haystack, $n)) {
                return true;
            }
        }
        return false;
    }
    
    function strFinish($value, $cap)
    {
        return rtrim($value, $cap) . $cap;
    }
    
    function strObject($value)
    {
        return is_object($value) && method_exists($value, '__toString');
    }
    
    function value($value)
    {
        return (is_callable($value) && !is_string($value)) ? call_user_func($value) : $value;
    }
    
    function instance($object)
    {
        return $object;
    }
    
    function versionPHP($version)
    {
        return version_compare(PHP_VERSION, $version) >= 0;
    }
    
    function strReplaceFirst($search, $replace, $subject) 
    {
        return implode($replace, explode($search, $subject, 2));
    }
    
    function arrayToAttr($attr)
	{
		$attr_str = '';

		if (!is_array($attr)) {
			$attr = (array) $attr;
		}

		foreach ($attr as $property => $value) {
			// Ignore null values
			if (is_null($value)) {
				continue;
			}

			// If the key is numeric then it must be something like selected="selected"
			if (is_numeric($property)) {
				$property = $value;
			}

			$attr_str .= $property . '="' . $value.'" ';
		}

		// We strip off the last space for return
		return trim($attr_str);
	}
    
    function htmlTag($tag, $attr = array(), $content = false)
	{
		$has_content = (bool) ($content !== false && $content !== null);
		$html = '<'.$tag;

		$html .= (!empty($attr)) ? ' ' . (is_array($attr) ? arrayToAttr($attr) : $attr) : '';
		$html .= $has_content ? '>' : ' />';
		$html .= $has_content ? $content.'</'.$tag.'>' : '';

		return $html;
	}
    
    function in_arrayi($needle, $haystack)
	{
		return in_array(strtolower($needle), array_map('strtolower', $haystack));
	}
    
    function e($string)
	{
		return \WZ_Inflector::htmlentities($string);
	}
    
    function getRealClass($class)
	{
		static $classes = array();
		if (!array_key_exists($class, $classes)) {
			$reflect = new \ReflectionClass($class);
			$classes[$class] = $reflect->getName();
		}
		return $classes[$class];
	}
    
    function getInstance()
    {
        return \WZ::getInstance();
    }
    
    function urlsite($echo = true)
    {
        if (true === $echo) {
            echo \WZ::get('urlsite');
        } else {
            return \WZ::get('urlsite');
        }
    }
    
    function __($str, $echo = true)
    {
        $t = \WZ::get('WZTranslate');
        $translation = $t->translate($str);
        if (true === $echo) {
            echo $translation;
        } else {
            return $translation;
        }
    }
    
    function ___($str)
    {
        return __($str, false);
    }